<?php
namespace util\ip;

use \artisan\input;
use \artisan\cache;
/**
 *IP地址 接口
 */
class IpAreaUtil{
    public  function ipArea($ip){
        $ip =trim($ip);
        //检查是否是有效的ip,排除内网ip等
        if ($ip) {
            $avail_ip = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE) ? true : false;
        }
        //无效IP和内网ip直接返回 false
        if ($avail_ip === true) {
            //发现是搜索引擎的ip，直接输出上海 
            if ($this->is_search_engine($ip)) {
                echo '上海';
                exit;
            }
            $url =  $this->get_url() . '&ip=' . $ip;
            $addressArr = json_decode(@file_get_contents($url), true);
            return $addressArr;
        }else{
            return false;
        }
    }
    private function get_url()
    {

        $keys = array('84c9191ee663321f020aa614e265a2e8', 'i9Tn5NuSj0GtZkygctd5rEyt');
        $rand = rand(0, 1);
        $key = $keys[$rand];
        $url = 'http://api.map.baidu.com/location/ip?ak=' . $key;
        return $url;
    }
    /**
     * 搜索引擎ip
     * 220.181.38.*;220.181.19.*;159.226.50.*;202.108.11.*;202.108.22.*;202.108.23.*;202.108.249.*;202.108.250.*;61.135.145.*;61.135.146.*;216.239.33.*;216.239.35.* ;216.239.37.*;216.239.39.*;216.239.51.*;216.239.53.*;216.239.55.*;216.239.57.*;216.239.59.*;64.233.161.*;64.233.189.*;66.102.11.*;66.102.7.*;66.102.9.*;66.249.64.*;66.249.65.*;66.249.66.*;66.249.71.*;66.249.72.*;72.14.207.*;202.106.186.*;202.108.36.*;202.108.44.*;202.108.45.*;202.108.5.*;202.108.9.*;220.181.12.*;220.181.13.*;220.181.14.*;220.181.15.*;220.181.28.*;220.181.31.*;222.185.245.*;66.196.90.* ;66.196.91.* ;68.142.249.* ;68.142.250.* ;68.142.251.*;72.30.101.* ;72.30.102.*;72.30.103.* ;72.30.104.* ;72.30.107.* ;72.30.110.* ;72.30.111.* ;72.30.128.* ;72.30.129.* ;72.30.131.* ;72.30.133.* ;72.30.134.* ;72.30.135.* ;72.30.216.* ;72.30.226.* ;72.30.252.* ;72.30.97.* ;72.30.98.* ;72.30.99.* ;74.6.74.*;202.165.102.* ;202.160.178.* ;202.160.179.* ;202.160.180.* ;202.160.181.* ;202.160.183.* ;202.108.4.*;202.108.4.*;202.108.33.*;202.96.51.* ;219.142.53.*
     */
    private function is_search_engine($ip)
    {
        $temp = explode('.', $ip);
        $prefix = $temp[0] . '.' . $temp[1] . '.' . $temp[2];
        $arr[] = '220.181.38.*';
        $arr[] = '220.181.19.*';
        $arr[] = '159.226.50.*';
        $arr[] = '202.108.11.*';
        $arr[] = '202.108.22.*';
        $arr[] = '202.108.23.*';
        $arr[] = '202.108.249.*';
        $arr[] = '202.108.250.*';
        $arr[] = '61.135.145.*';
        $arr[] = '61.135.146.*';
        $arr[] = '216.239.33.*';
        $arr[] = '216.239.35.* ';
        $arr[] = '216.239.37.*';
        $arr[] = '216.239.39.*';
        $arr[] = '216.239.51.*';
        $arr[] = '216.239.53.*';
        $arr[] = '216.239.55.*';
        $arr[] = '216.239.57.*';
        $arr[] = '216.239.59.*';
        $arr[] = '64.233.161.*';
        $arr[] = '64.233.189.*';
        $arr[] = '66.102.11.*';
        $arr[] = '66.102.7.*';
        $arr[] = '66.102.9.*';
        $arr[] = '66.249.64.*';
        $arr[] = '66.249.65.*';
        $arr[] = '66.249.66.*';
        $arr[] = '66.249.71.*';
        $arr[] = '66.249.72.*';
        $arr[] = '72.14.207.*';
        $arr[] = '202.106.186.*';
        $arr[] = '202.108.36.*';
        $arr[] = '202.108.44.*';
        $arr[] = '202.108.45.*';
        $arr[] = '202.108.5.*';
        $arr[] = '202.108.9.*';
        $arr[] = '220.181.12.*';
        $arr[] = '220.181.13.*';
        $arr[] = '220.181.14.*';
        $arr[] = '220.181.15.*';
        $arr[] = '220.181.28.*';
        $arr[] = '220.181.31.*';
        $arr[] = '222.185.245.*';
        $arr[] = '66.196.90.* ';
        $arr[] = '66.196.91.* ';
        $arr[] = '68.142.249.* ';
        $arr[] = '68.142.250.* ';
        $arr[] = '68.142.251.*';
        $arr[] = '72.30.101.* ';
        $arr[] = '72.30.102.*';
        $arr[] = '72.30.103.* ';
        $arr[] = '72.30.104.* ';
        $arr[] = '72.30.107.* ';
        $arr[] = '72.30.110.* ';
        $arr[] = '72.30.111.* ';
        $arr[] = '72.30.128.* ';
        $arr[] = '72.30.129.* ';
        $arr[] = '72.30.131.* ';
        $arr[] = '72.30.133.* ';
        $arr[] = '72.30.134.* ';
        $arr[] = '72.30.135.* ';
        $arr[] = '72.30.216.* ';
        $arr[] = '72.30.226.* ';
        $arr[] = '72.30.252.* ';
        $arr[] = '72.30.97.* ';
        $arr[] = '72.30.98.* ';
        $arr[] = '72.30.99.* ';
        $arr[] = '74.6.74.*';
        $arr[] = '202.165.102.* ';
        $arr[] = '202.160.178.* ';
        $arr[] = '202.160.179.* ';
        $arr[] = '202.160.180.* ';
        $arr[] = '202.160.181.* ';
        $arr[] = '202.160.183.* ';
        $arr[] = '202.108.4.*';
        $arr[] = '202.108.4.*';
        $arr[] = '202.108.33.*';
        $arr[] = '202.96.51.* ';
        $arr[] = '219.142.53.*';
        foreach ($arr as $one) {
            if (strpos($one, $prefix) === 0) {
                return true;
            }
        }
        return false;
    }
}